#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/bk/apis/cc/enter_ip.py
# Compiled at: 2017-11-16 15:44:28
"""
Copyright © 2012-2017 Tencent BlueKing. All Rights Reserved. 蓝鲸智云 版权所有
"""
from django import forms
from common.forms import BaseComponentForm, ListField
from common.constants import API_TYPE_OP
from components.component import Component
from .toolkit import tools, configs

class EnterIp(Component):
    """
    apiLabel 导入主机到业务
    apiMethod POST
    
    ### 功能描述
    
    将主机导入到业务下面。如果业务不存在，将导入到资源池中，如果主机已经存在，将会删除原有主机与模块的关系
    
    ### 请求参数
    
    {{ common_args_desc }}
    
    #### 接口参数
    
    | 字段      |  类型      | 必选   |  描述      |
    |-----------|------------|--------|------------|
    | ips       |  string    | 是     | IP地址，多个用英文逗号分隔 |
    | hostname  |  string    | 否     | 主机名, 多个用英文逗号分隔 |
    | app_name  |  string    | 否     | 业务名 |
    | set_name  |  string    | 否     | 集群名 |
    | module_name |  string  | 否     | 模块名 |
    | os_type   |  string    | 否     | 操作系统类型，linux或windows |
    
    ### 请求参数示例
    
    ```python
    {
        "app_code": "esb_test",
        "app_secret": "xxx",
        "bk_token": "xxx",
        "ips": "10.10.10.10,10.10.10.11",
        "hostname": "test",
        "app_name": "test",
        "set_name": "test",
        "module_name": "test",
    }
    ```
    
    ### 返回结果示例
    
    ```python
    {
        "result": true,
        "code": "00",
        "message": "",
        "data": null
    }
    ```
    """
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_OP
    host = configs.host

    class Form(BaseComponentForm):
        ips = ListField(label=u'IP地址', required=True)
        hostname = ListField(label=u'主机名', required=False)
        app_name = forms.CharField(label=u'业务名', required=False)
        set_name = forms.CharField(label=u'集群名', required=False)
        module_name = forms.CharField(label=u'模块名', required=False)
        os_type = forms.CharField(label=u'操作系统类型', required=False)

        def clean(self):
            data = self.cleaned_data
            return {'ip': (',').join(data['ips']),
               'hostname': (',').join(data['hostname']),
               'appName': data['app_name'],
               'setName': data['set_name'],
               'moduleName': data['module_name'],
               'osType': data['os_type']
               }

    def handle(self):
        client = tools.CCClient(self.outgoing.http_client)
        self.response.payload = client.post_request(self.host, '/api/host/enterIp', data=self.form_data)
